<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * 模块:用户登陆登出模块
 * 开发:胡耆英
 * 创建日期: 2011-05-15
 * 
 */
class Login extends CI_Controller 
{
    private $data;
    public function __construct()
    {
        parent::__construct();
        $this->data['title'] = "广州市巴古服饰有限公司管理系统--登录!";
    }
    /*
     * 用户登陆
     */
    public function index()
    {    
		if($this->check_login())
        {
            redirect('admin');
            return false;
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', '用户名', 'required|min_length[2]|max_length[25]|trim');
		$this->form_validation->set_rules('password', '密码', 'required|min_length[5]|max_length[25]|trim');
		if($this->form_validation->run() === FALSE)
		{
			 $this->load->view('login',$this->data);
		}
        else
        {
           $username = $this->input->post('username');
           $password = md5($this->input->post('password'));
           $this->db->select('*')
                    ->from('admin')
                    ->where(array('username'=>$username,'password'=>$password))
                    ->limit(1);
           $query = $this->db->get();
           if($query->num_rows()==1)
           {
                $data = $query->row_array();
                $session_arr = array(
                   'id'=>$data['id'],
                   'username' => $data['username'],
                   'role' => $data['gid'],
                );
                $this->session->set_userdata($session_arr);
                $this->db->where('id', $data['id']);
                $this->db->update('admin',array('datetime'=>time()));
                redirect('admin');
           }
           else
           {
               show_message('用户名或者密码错误,请确认后重新登录!', site_url('login'),1);
           }
        }
    }
    
    /*
     * 用户登出
     */
    public function login_out()
    {   
       $this->session->sess_destroy();
       redirect('login');
    }   
    
    /*
     * 检查登陆
     */
    protected function check_login()
    {
        return $this->session->userdata('id')!=''?true:false;
    } 
}
?>
